package com.sugar.real.estate.module.business.service.infocls;

import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.module.business.controller.admin.infocls.vo.InfoClsPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.infocls.vo.InfoClsSaveReqVO;
import com.sugar.real.estate.module.business.controller.admin.infocls.vo.InfoClsSimpleRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.infocls.InfoClsDO;
import com.sugar.real.estate.module.business.dal.mysql.infocls.InfoClsMapper;
import com.sugar.real.estate.module.business.util.I18nUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.sugar.real.estate.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.real.estate.module.business.enums.ErrorCodeConstants.INFO_CLS_NOT_EXISTS;

/**
 * 资讯分类 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class InfoClsServiceImpl implements InfoClsService {

    @Resource
    private InfoClsMapper infoClsMapper;

    @Override
    public Long createInfoCls(InfoClsSaveReqVO createReqVO) {
        // 插入
        InfoClsDO infoCls = BeanUtils.toBean(createReqVO, InfoClsDO.class);
        infoClsMapper.insert(infoCls);
        // 返回
        return infoCls.getId();
    }

    @Override
    public void updateInfoCls(InfoClsSaveReqVO updateReqVO) {
        // 校验存在
        validateInfoClsExists(updateReqVO.getId());
        // 更新
        InfoClsDO updateObj = BeanUtils.toBean(updateReqVO, InfoClsDO.class);
        infoClsMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfoCls(Long id) {
        // 校验存在
        validateInfoClsExists(id);
        // 删除
        infoClsMapper.deleteById(id);
    }

    private void validateInfoClsExists(Long id) {
        if (infoClsMapper.selectById(id) == null) {
            throw exception(INFO_CLS_NOT_EXISTS);
        }
    }

    @Override
    public InfoClsDO getInfoCls(Long id) {
        return infoClsMapper.selectById(id);
    }

    @Override
    public List<InfoClsSimpleRespVO> getInfoClsAll() {
        List<InfoClsDO> infoClsList = infoClsMapper.selectList();
        return BeanUtils.toBean(infoClsList, InfoClsSimpleRespVO.class, o -> o.setTitle(o.getTitleZhCn() + "(" + o.getTitleEnUs() + ")"));
    }

    @Override
    public PageResult<InfoClsDO> getInfoClsPage(InfoClsPageReqVO pageReqVO) {
        return infoClsMapper.selectPage(pageReqVO);
    }

    // ======================================== APP 接口 ========================================

    @Override
    public List<InfoClsSimpleRespVO> appGetInfoClsAll() {
        List<InfoClsDO> infoClsList = infoClsMapper.selectList();
        return BeanUtils.toBean(infoClsList, InfoClsSimpleRespVO.class, o -> o.setTitle(I18nUtil.getMessage(o, "getTitle")));
    }
}